package com.service.ljl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dao.ljl.payplandao;
import com.dao.ljl.payplanmybitisdao;
import com.dao.ljl.payrecorddao;
import com.entity.Billrecord;
import com.entity.Payplan;
import com.entity.Payrecord;
import com.service.ServiceException;

@Service
@Transactional(rollbackFor = ServiceException.class)
public class payrecordservice {
	@Autowired
	private payrecorddao payrecorddao;
	@Autowired
	private payplandao payplandao;
	@Autowired
	private payplanmybitisdao payplanmybitisdao;

	public void add(Map<Object, Object> map) {
		Payrecord payrecord = JSONObject.parseObject(map.get("payrecord").toString(), Payrecord.class);
		Long id = JSONObject.parseObject(map.get("id").toString(), Long.class);
		payrecord.setPayplan(payplandao.findOne(id));
		payrecorddao.save(payrecord);
	}

	public Map<Object, Object> findall(int pagesize, int currage) {
		Map<Object, Object> map = new HashMap<>();
		List<Payrecord> li = payplanmybitisdao.findrecord();
		int start = (currage - 1) * pagesize;
		int finish = currage * pagesize;
		if (finish > li.size()) {
			finish = li.size();
		}
		for (int i = 0; i < li.size(); i++) {
			if (li.get(i).getBillrecords().size() > 0)
				li.get(i).setPcBf(new BigDecimal(3));
		}
		map.put("payrecords", li.subList(start, finish));
		map.put("total", li.size());
		return map;
	}

	public void delete(Long id) {
		payrecorddao.delete(id);
	}

	public void deletes(List<Long> ids) {
		for (int i = 0; i < ids.size(); i++) {
			payrecorddao.delete(ids.get(i));
		}
	}

	public Map<Object, Object> findallbill(int pagesize, int currage) {
		Map<Object, Object> map = new HashMap<>();
		List<Billrecord> li = payplanmybitisdao.findbill();
		int start = (currage - 1) * pagesize;
		int finish = currage * pagesize;
		if (finish > li.size()) {
			finish = li.size();
		}

		map.put("billcords", li.subList(start, finish));
		map.put("total", li.size());
		return map;
	}

}
